package com.xunji.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunji.standard.entity.TXjNdFlBankInformation;
import com.xunji.standard.entity.TXjNdFlBillInformation;
import com.xunji.standard.mapper.TXjNdFlBillInformationMapper;
import com.xunji.standard.service.TXjNdFlBillInformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【t_xj_nd_fl_bill_information】的数据库操作Service实现
 * @createDate 2025-08-18 14:38:40
 */
@Service
public class TXjNdFlBillInformationServiceImpl extends ServiceImpl<TXjNdFlBillInformationMapper, TXjNdFlBillInformation> implements TXjNdFlBillInformationService {
    @Resource
    TXjNdFlBankInformationServiceImpl tXjNdFlBankInformationService;

    @Override
    public List<TXjNdFlBillInformation> selectListByCaseIdAndInfoId(String caseId, Long infoId, String xm, String sfzh) {
        List<String> khList = getKhList(caseId, xm, sfzh);
        if (khList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<TXjNdFlBillInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId).in("kh", khList);
        return this.baseMapper.selectList(qw);
    }

    @Override
    public List<TXjNdFlBillInformation> selectListByCaseIdAndXmAndSfzh(String caseId, String xm, String sfzh) {
        List<String> khList = getKhList(caseId, xm, sfzh);
        if (khList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<TXjNdFlBillInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId).in("kh", khList);
        return this.baseMapper.selectList(qw);
    }


    @Override
    public void deleteByCaseId(String caseId, String xm, String sfzh) {
        List<String> khList = getKhList(caseId, xm, sfzh);
        if (khList.isEmpty()) {
            return;
        }
        QueryWrapper<TXjNdFlBillInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId).in("kh", khList);
        this.baseMapper.delete(qw);
    }

    @Override
    public List<TXjNdFlBillInformation> selectListByCaseId(String caseId) {
        QueryWrapper<TXjNdFlBillInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        return this.baseMapper.selectList(qw);
    }

    private List<String> getKhList(String caseId, String xm, String sfzh) {
        // 通过银行账户信息表查询卡号，再根据卡号查询流水
        List<TXjNdFlBankInformation> list = tXjNdFlBankInformationService.selectListByCaseIdAndInfoId(caseId, null, xm, sfzh);
        ArrayList<String> khList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (TXjNdFlBankInformation bi : list) {
                String kh = bi.getKh();
                if (!khList.contains(kh)) {
                    khList.add(kh);
                }
            }
        }
        return khList;
    }
}
